package dbcore

import (
	"fmt"
	"log"
	"runtime/debug"

	"gitee.com/gitee-go/core"
	_ "github.com/go-sql-driver/mysql"
	"xorm.io/xorm"
)

/*
InitMysql init mysql
*/
func InitMysql(url string, ShowSQL ...bool) (*xorm.Engine, error) {
	defer func() {
		if err := recover(); err != nil {
			core.LogPnc.Errorf("InitMysql:%+v", err)
			core.LogPnc.Errorf("%s", string(debug.Stack()))
		}
	}()

	db, err := xorm.NewEngine("mysql", url)
	if err != nil {
		log.Printf(fmt.Sprintf("db init failed : %s", err.Error()))
		return nil, err
	}

	//db.SetLogger(core.Log)
	if ShowSQL != nil && len(ShowSQL) > 0 {
		if ShowSQL[0] {
			db.ShowSQL(true)
		}
	}

	return db, nil

}
